/* Copyright (C) 2005-2013, Unigine Corp. All rights reserved.
 *
 * File:    fragment_triplanar_sample.h
 * Desc:    Mesh triplanar sample shader
 * Version: 1.03
 * Author:  Alexander Zapryagaev <frustum@unigine.com>
 *
 * This file is part of the Unigine engine (http://unigine.com/).
 *
 * Your use and or redistribution of this software in source and / or
 * binary form, with or without modification, is subject to: (i) your
 * ongoing acceptance of and compliance with the terms and conditions of
 * the Unigine License Agreement; and (ii) your inclusion of this notice
 * in any version of this software that you use or redistribute.
 * A copy of the Unigine License Agreement is available by contacting
 * Unigine Corp. at http://unigine.com/
 */

/******************************************************************************\
*
* OpenGL
*
\******************************************************************************/

#ifdef OPENGL
	
	#ifdef USE_ALPHA_FADE && ALPHA_FADE
		texture2DAlphaFadeDiscard(s_texture_10,s_texcoord_9.x);
	#endif
	
	half3 weight = abs(half3(s_texcoord_0.xy,s_texcoord_6.w));
	weight = pow(weight,half3(triplanar_power));
	weight /= dot(weight,half3(1.0f));
	
	#ifdef SEPARABLE
		
		float2 texcoord_yz = s_texcoord_6.yz * base_0_transform.xy + base_0_transform.zw;
		float2 texcoord_xz = s_texcoord_6.xz * base_1_transform.xy + base_1_transform.zw;
		float2 texcoord_xy = s_texcoord_6.xy * base_2_transform.xy + base_2_transform.zw;
		
		half4 diffuse_0 = texture(s_texture_0,texcoord_yz) * weight.x;
		half4 diffuse_1 = texture(s_texture_1,texcoord_xz) * weight.y;
		half4 diffuse_2 = texture(s_texture_2,texcoord_xy) * weight.z;
		
		half3 normal_0 = texture2DNormal(s_texture_3,texcoord_yz) * weight.x;
		half3 normal_1 = texture2DNormal(s_texture_4,texcoord_xz) * weight.y;
		half3 normal_2 = texture2DNormal(s_texture_5,texcoord_xy) * weight.z;
		
		half4 specular_0 = texture(s_texture_6,texcoord_yz) * weight.x;
		half4 specular_1 = texture(s_texture_7,texcoord_xz) * weight.y;
		half4 specular_2 = texture(s_texture_8,texcoord_xy) * weight.z;
		
	#else
		
		float2 texcoord_yz = s_texcoord_6.yz * base_0_transform.xy + base_0_transform.zw;
		float2 texcoord_xz = s_texcoord_6.xz * base_0_transform.xy + base_0_transform.zw;
		float2 texcoord_xy = s_texcoord_6.xy * base_0_transform.xy + base_0_transform.zw;
		
		half4 diffuse_0 = texture(s_texture_0,texcoord_yz) * weight.x;
		half4 diffuse_1 = texture(s_texture_0,texcoord_xz) * weight.y;
		half4 diffuse_2 = texture(s_texture_0,texcoord_xy) * weight.z;
		
		half3 normal_0 = texture2DNormal(s_texture_3,texcoord_yz) * weight.x;
		half3 normal_1 = texture2DNormal(s_texture_3,texcoord_xz) * weight.y;
		half3 normal_2 = texture2DNormal(s_texture_3,texcoord_xy) * weight.z;
		
		half4 specular_0 = texture(s_texture_6,texcoord_yz) * weight.x;
		half4 specular_1 = texture(s_texture_6,texcoord_xz) * weight.y;
		half4 specular_2 = texture(s_texture_6,texcoord_xy) * weight.z;
		
	#endif
	
	normal_0.y = -normal_0.y;
	normal_1.y = -normal_1.y;
	
	normal_0.x *= sign(s_texcoord_0.x);
	normal_1.x *= -sign(s_texcoord_0.y);
	normal_2.y *= -sign(s_texcoord_6.w);
	
	half4 diffuse = (diffuse_0 + diffuse_1 + diffuse_2) * diffuse_color;
	half3 normal = normalize(normal_0 + normal_1 + normal_2);
	half4 specular = (specular_0 + specular_1 + specular_2) * specular_color;
	
/******************************************************************************\
*
* Direct3D11
*
\******************************************************************************/

#elif DIRECT3D11
	
	#ifdef USE_ALPHA_FADE && ALPHA_FADE
		texture2DAlphaFadeDiscard(s_texture_10,s_sampler_10,IN.position.xy,IN.texcoord_9.x);
	#endif
	
	half3 weight = abs(half3(IN.texcoord_0.xy,IN.texcoord_6.w));
	weight = pow(weight,triplanar_power);
	weight /= dot(weight,1.0f);
	
	#ifdef SEPARABLE
		
		float2 texcoord_yz = IN.texcoord_6.yz * base_0_transform.xy + base_0_transform.zw;
		float2 texcoord_xz = IN.texcoord_6.xz * base_1_transform.xy + base_1_transform.zw;
		float2 texcoord_xy = IN.texcoord_6.xy * base_2_transform.xy + base_2_transform.zw;
		
		half4 diffuse_0 = s_texture_0.Sample(s_sampler_0,texcoord_yz) * weight.x;
		half4 diffuse_1 = s_texture_1.Sample(s_sampler_1,texcoord_xz) * weight.y;
		half4 diffuse_2 = s_texture_2.Sample(s_sampler_2,texcoord_xy) * weight.z;
		
		half3 normal_0 = texture2DNormal(s_texture_3,s_sampler_3,texcoord_yz) * weight.x;
		half3 normal_1 = texture2DNormal(s_texture_4,s_sampler_4,texcoord_xz) * weight.y;
		half3 normal_2 = texture2DNormal(s_texture_5,s_sampler_5,texcoord_xy) * weight.z;
		
		half4 specular_0 = s_texture_6.Sample(s_sampler_6,texcoord_yz) * weight.x;
		half4 specular_1 = s_texture_7.Sample(s_sampler_7,texcoord_xz) * weight.y;
		half4 specular_2 = s_texture_8.Sample(s_sampler_8,texcoord_xy) * weight.z;
		
	#else
		
		float2 texcoord_yz = IN.texcoord_6.yz * base_0_transform.xy + base_0_transform.zw;
		float2 texcoord_xz = IN.texcoord_6.xz * base_0_transform.xy + base_0_transform.zw;
		float2 texcoord_xy = IN.texcoord_6.xy * base_0_transform.xy + base_0_transform.zw;
		
		half4 diffuse_0 = s_texture_0.Sample(s_sampler_0,texcoord_yz) * weight.x;
		half4 diffuse_1 = s_texture_0.Sample(s_sampler_0,texcoord_xz) * weight.y;
		half4 diffuse_2 = s_texture_0.Sample(s_sampler_0,texcoord_xy) * weight.z;
		
		half3 normal_0 = texture2DNormal(s_texture_3,s_sampler_3,texcoord_yz) * weight.x;
		half3 normal_1 = texture2DNormal(s_texture_3,s_sampler_3,texcoord_xz) * weight.y;
		half3 normal_2 = texture2DNormal(s_texture_3,s_sampler_3,texcoord_xy) * weight.z;
		
		half4 specular_0 = s_texture_6.Sample(s_sampler_6,texcoord_yz) * weight.x;
		half4 specular_1 = s_texture_6.Sample(s_sampler_6,texcoord_xz) * weight.y;
		half4 specular_2 = s_texture_6.Sample(s_sampler_6,texcoord_xy) * weight.z;
		
	#endif
	
	normal_0.y = -normal_0.y;
	normal_1.y = -normal_1.y;
	
	normal_0.x *= sign(IN.texcoord_0.x);
	normal_1.x *= -sign(IN.texcoord_0.y);
	normal_2.y *= -sign(IN.texcoord_6.w);
	
	half4 diffuse = (diffuse_0 + diffuse_1 + diffuse_2) * diffuse_color;
	half3 normal = normalize(normal_0 + normal_1 + normal_2);
	half4 specular = (specular_0 + specular_1 + specular_2) * specular_color;
	
/******************************************************************************\
*
* Direct3D9
*
\******************************************************************************/

#elif DIRECT3D9
	
	#ifdef USE_ALPHA_FADE && ALPHA_FADE
		tex2DAlphaFadeDiscard(s_texture_10,IN.texcoord_7,IN.texcoord_7.z);
	#endif
	
	half3 weight = abs(half3(IN.texcoord_0.xy,IN.texcoord_6.w));
	weight = pow(weight,triplanar_power);
	weight /= dot(weight,1.0f);
	
	#ifdef SEPARABLE
		
		float2 texcoord_yz = IN.texcoord_6.yz * base_0_transform.xy + base_0_transform.zw;
		float2 texcoord_xz = IN.texcoord_6.xz * base_1_transform.xy + base_1_transform.zw;
		float2 texcoord_xy = IN.texcoord_6.xy * base_2_transform.xy + base_2_transform.zw;
		
		half4 diffuse_0 = tex2D(s_texture_0,texcoord_yz) * weight.x;
		half4 diffuse_1 = tex2D(s_texture_1,texcoord_xz) * weight.y;
		half4 diffuse_2 = tex2D(s_texture_2,texcoord_xy) * weight.z;
		
		half3 normal_0 = tex2DNormal(s_texture_3,texcoord_yz) * weight.x;
		half3 normal_1 = tex2DNormal(s_texture_4,texcoord_xz) * weight.y;
		half3 normal_2 = tex2DNormal(s_texture_5,texcoord_xy) * weight.z;
		
		half4 specular_0 = tex2D(s_texture_6,texcoord_yz) * weight.x;
		half4 specular_1 = tex2D(s_texture_7,texcoord_xz) * weight.y;
		half4 specular_2 = tex2D(s_texture_8,texcoord_xy) * weight.z;
		
	#else
		
		float2 texcoord_yz = IN.texcoord_6.yz * base_0_transform.xy + base_0_transform.zw;
		float2 texcoord_xz = IN.texcoord_6.xz * base_0_transform.xy + base_0_transform.zw;
		float2 texcoord_xy = IN.texcoord_6.xy * base_0_transform.xy + base_0_transform.zw;
		
		half4 diffuse_0 = tex2D(s_texture_0,texcoord_yz) * weight.x;
		half4 diffuse_1 = tex2D(s_texture_0,texcoord_xz) * weight.y;
		half4 diffuse_2 = tex2D(s_texture_0,texcoord_xy) * weight.z;
		
		half3 normal_0 = tex2DNormal(s_texture_3,texcoord_yz) * weight.x;
		half3 normal_1 = tex2DNormal(s_texture_3,texcoord_xz) * weight.y;
		half3 normal_2 = tex2DNormal(s_texture_3,texcoord_xy) * weight.z;
		
		half4 specular_0 = tex2D(s_texture_6,texcoord_yz) * weight.x;
		half4 specular_1 = tex2D(s_texture_6,texcoord_xz) * weight.y;
		half4 specular_2 = tex2D(s_texture_6,texcoord_xy) * weight.z;
		
	#endif
	
	normal_0.y = -normal_0.y;
	normal_1.y = -normal_1.y;
	
	normal_0.x *= sign(IN.texcoord_0.x);
	normal_1.x *= -sign(IN.texcoord_0.y);
	normal_2.y *= -sign(IN.texcoord_6.w);
	
	half4 diffuse = (diffuse_0 + diffuse_1 + diffuse_2) * diffuse_color;
	half3 normal = normalize(normal_0 + normal_1 + normal_2);
	half4 specular = (specular_0 + specular_1 + specular_2) * specular_color;
	
/******************************************************************************\
*
* PlayStation3
*
\******************************************************************************/

#elif PLAYSTATION3
	
	#ifdef USE_ALPHA_FADE && ALPHA_FADE
		tex2DAlphaFadeDiscard(s_texture_10,IN.texcoord_9,IN.texcoord_9.z);
	#endif
	
	half3 weight = abs(half3(IN.texcoord_0.xy,IN.texcoord_8.w));
	weight = pow(weight,triplanar_power);
	weight /= dot(weight,1.0f);
	
	#ifdef SEPARABLE
		
		float2 texcoord_yz = IN.texcoord_8.yz * base_0_transform.xy + base_0_transform.zw;
		float2 texcoord_xz = IN.texcoord_8.xz * base_1_transform.xy + base_1_transform.zw;
		float2 texcoord_xy = IN.texcoord_8.xy * base_2_transform.xy + base_2_transform.zw;
		
		half4 diffuse_0 = h4tex2D(s_texture_0,texcoord_yz) * weight.x;
		half4 diffuse_1 = h4tex2D(s_texture_1,texcoord_xz) * weight.y;
		half4 diffuse_2 = h4tex2D(s_texture_2,texcoord_xy) * weight.z;
		
		half3 normal_0 = tex2DNormal(s_texture_3,texcoord_yz) * weight.x;
		half3 normal_1 = tex2DNormal(s_texture_4,texcoord_xz) * weight.y;
		half3 normal_2 = tex2DNormal(s_texture_5,texcoord_xy) * weight.z;
		
		half4 specular_0 = h4tex2D(s_texture_6,texcoord_yz) * weight.x;
		half4 specular_1 = h4tex2D(s_texture_7,texcoord_xz) * weight.y;
		half4 specular_2 = h4tex2D(s_texture_8,texcoord_xy) * weight.z;
		
	#else
		
		float2 texcoord_yz = IN.texcoord_8.yz * base_0_transform.xy + base_0_transform.zw;
		float2 texcoord_xz = IN.texcoord_8.xz * base_0_transform.xy + base_0_transform.zw;
		float2 texcoord_xy = IN.texcoord_8.xy * base_0_transform.xy + base_0_transform.zw;
		
		half4 diffuse_0 = h4tex2D(s_texture_0,texcoord_yz) * weight.x;
		half4 diffuse_1 = h4tex2D(s_texture_0,texcoord_xz) * weight.y;
		half4 diffuse_2 = h4tex2D(s_texture_0,texcoord_xy) * weight.z;
		
		half3 normal_0 = tex2DNormal(s_texture_3,texcoord_yz) * weight.x;
		half3 normal_1 = tex2DNormal(s_texture_3,texcoord_xz) * weight.y;
		half3 normal_2 = tex2DNormal(s_texture_3,texcoord_xy) * weight.z;
		
		half4 specular_0 = h4tex2D(s_texture_6,texcoord_yz) * weight.x;
		half4 specular_1 = h4tex2D(s_texture_6,texcoord_xz) * weight.y;
		half4 specular_2 = h4tex2D(s_texture_6,texcoord_xy) * weight.z;
		
	#endif
	
	normal_0.y = -normal_0.y;
	normal_1.y = -normal_1.y;
	
	normal_0.x *= sign(IN.texcoord_0.x);
	normal_1.x *= -sign(IN.texcoord_0.y);
	normal_2.y *= -sign(IN.texcoord_8.w);
	
	half4 diffuse = (diffuse_0 + diffuse_1 + diffuse_2) * diffuse_color;
	half3 normal = normalize(normal_0 + normal_1 + normal_2);
	half4 specular = (specular_0 + specular_1 + specular_2) * specular_color;
	
#endif
